//必须带done回调 执行完成调用
import { ElMessage } from 'element-plus'
import cloneDeep from 'lodash/cloneDeep'
import trim from 'lodash/trim'
import isNumber from 'lodash/isNumber'
import isEmpty from 'lodash/isEmpty'
import { translateTitle } from '@/utils/i18n'

export default function cacheAdd({ vm, action, done, callbackData }) {
  // console.log('getData', module, action)
  const cacheValueModel = action.extension.cacheValueModel.value[0]
  const matchField = action.extension.matchField.value
  const numberField = action.extension.numberField.value
  const getDataPath = action.extension.getDataPath.value

  // 先获取之前的缓存值
  if (!cacheValueModel) {
    ElMessage({
      type: 'warning',
      message: translateTitle('Ve.eventTask.请选择匹配值组件'),
    })
    done(null, {})
    return
  }

  const cacheValueModelSelf = vm.vuiComponents[cacheValueModel] && vm.vuiComponents[cacheValueModel].self
  if (!cacheValueModelSelf) {
    ElMessage({
      type: 'warning',
      message: translateTitle('Ve.eventTask.请选择正确的匹配值组件'),
    })
    done(null, {})
    return
  }
  // console.log('cacheValueModelSelf ------ ', cacheValueModelSelf)
  let _callbackData = cloneDeep(callbackData)
  if (trim(getDataPath)) {
    // 如果配置了获取数据路径 则通过路径获取正确的数据
    if (getDataPath.indexOf('->') !== -1) {
      const fieldList = getDataPath.split('->')
      // window.cacheObj[b] = _callbackData
      fieldList.forEach((fieldItem) => {
        // 如果没有后续值则不继续往下取值
        if (_callbackData[trim(fieldItem)] === undefined) return
        _callbackData = _callbackData[trim(fieldItem)]
      })
    } else {
      _callbackData = _callbackData[trim(getDataPath)] || {}
    }
  }
  if (cacheValueModelSelf.info.type === 'uiTable') {
    // 如果选择的是表格组件 则获取表格数据
    let valueList = cloneDeep(cacheValueModelSelf.info.data.requestData.data || [])
    let cacheValueObj = {}
    valueList.forEach((item) => {
      // 查找对应的缓存数据
      if (item[matchField] === _callbackData[matchField]) {
        cacheValueObj = item
      }
    })
    // 如果没有在当前缓存列表里面匹配到值 则使用上次任务数据
    if (isEmpty(cacheValueObj)) {
      cacheValueObj = _callbackData
      valueList.push(cacheValueObj)
    }

    // 然后再进行数量的累加
    if (cacheValueObj[numberField] && isNumber(cacheValueObj[numberField])) {
      cacheValueObj[numberField]++
    } else {
      cacheValueObj[numberField] = 1
    }

    // 重新赋值表格数据
    cacheValueModelSelf.info.data.requestData.data = valueList
    // 存储动作数据
    vm.$store.dispatch('actionDatas/setActionsData', {
      moduleId: vm.info._id,
      actionId: action._id,
      actionData: cacheValueObj,
    })
    done(null, cacheValueObj)
  } else if (cacheValueModelSelf.info.type === 'uiForm') {
    // 如果选择的是表单组件
    let cacheValueObj = cacheValueModelSelf.info.data.formData
    // 进行数量的累加
    if (cacheValueObj[numberField] && isNumber(cacheValueObj[numberField])) {
      cacheValueObj[numberField]++
    } else {
      cacheValueObj[numberField] = 1
    }
    // 存储动作数据
    vm.$store.dispatch('actionDatas/setActionsData', {
      moduleId: vm.info._id,
      actionId: action._id,
      actionData: cacheValueObj,
    })
    done(null, cacheValueObj)
  } else {
    done(null, {})
  }
}
